package retrofitdemo.com.night.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ObservableField;
import androidx.fragment.app.Fragment;

import com.lzy.okgo.callback.StringCallback;

import okhttp3.Call;
import okhttp3.Response;
import retrofitdemo.com.night.R;
import retrofitdemo.com.night.activity.LoginActivity;
import retrofitdemo.com.night.activity.SignInActivity;
import retrofitdemo.com.night.bean.ReasionInfoBean;
import retrofitdemo.com.night.databinding.FragmentSecondBinding;
import retrofitdemo.com.night.utils.GsonUtil;
import retrofitdemo.com.night.utils.HTTPAPI;
import retrofitdemo.com.night.utils.LogUtils;
import retrofitdemo.com.night.utils.SPUtil;

public class SecondFragment extends Fragment implements View.OnClickListener {

    FragmentSecondBinding binding;
    public ObservableField<String> FBI_NAME = new ObservableField<>();
    public ObservableField<String> FBI_PHONE = new ObservableField<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(getActivity()), R.layout.fragment_second,container,false);
        //页面数据初始化方法
        disposeData();
        binding.setFragment(this);
        return binding.getRoot();
    }

    private boolean mB_dataFirst = true;
    private void disposeData(){
        if (mB_dataFirst){
            initData();
            mB_dataFirst = !mB_dataFirst;
        }
        refreshData();
    }

    /**
     * 初始化数据
     */
    public void initData(){
        String name = (String) SPUtil.get(getContext(),"module_name","");
        String phone = (String) SPUtil.get(getContext(),"module_phone","");
        FBI_NAME.set(name);
        FBI_PHONE.set(phone);
        binding.tvModuleSign.setOnClickListener(this);
        binding.llModuleSign.setOnClickListener(this);
        binding.llModuleCollect.setOnClickListener(this);
        binding.tvModuleGetOut.setOnClickListener(this);
    }

    /**
     * 刷新数据
     */
    public void refreshData(){
        LogUtils.e("刷新");
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tvModuleSign) {
            playCar();
        }else if (id == R.id.llModuleCollect){
            startActivity(new Intent(getActivity(), SignInActivity.class).putExtra("module_type","collect"));
        }else if (id == R.id.llModuleSign){
            startActivity(new Intent(getActivity(), SignInActivity.class).putExtra("module_type","signIn"));
        }else if (id == R.id.tvModuleGetOut){
            SPUtil.clear(getActivity());
            startActivity(new Intent(getActivity(), LoginActivity.class));
            getActivity().finish();
        }
    }

    public void playCar(){
        HTTPAPI.getInstance().playCar(getActivity(),new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("签到-->"+ s);
                Toast.makeText(getActivity(),"签到成功",Toast.LENGTH_SHORT).show();
            }
        });
    }

    /**
     * 获取个人基本信息
     */
    public void getInfo(){
        HTTPAPI.getInstance().getInfo(getActivity(),new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                LogUtils.e("基本信息-->"+ s);
                ReasionInfoBean bean = GsonUtil.GsonToBean(s,ReasionInfoBean.class);
                if ("200".equals(bean.getCode())) {
                    FBI_NAME.set(bean.getResult().getName());
                    FBI_PHONE.set(bean.getResult().getMobile());
                }
            }
        });
    }
}
